package cluster

import akka.actor.{ActorPaths, ActorRef, ActorSystem}
import akka.cluster.client.ClusterClient.Send
import akka.cluster.client.{ClusterClient, ClusterClientSettings}
import com.typesafe.config.ConfigFactory

/**
 * @Description: TODO
 * @Author: DJJ
 * @Date: 2019/12/13 17:42
 */
object ClusterUtils {

  val config = ConfigFactory.parseString(
    s"""
       |akka.actor.provider="akka.remote.RemoteActorRefProvider"
       |akka.remote.netty.tcp.hostname="127.0.0.1"
       |akka.remote.netty.tcp.port=8080
        """.stripMargin)

  val sparkWoker = ActorSystem("ClusterWoker", config)
  val path1="akka.tcp://Cluster@127.0.0.1:9990/system/receptionist"
  val path2="akka.tcp://Cluster@127.0.0.1:9991/system/receptionist"
  val initialContacts= Set(ActorPaths.fromString(path1), ActorPaths.fromString(path2))
  private val actorRef: ActorRef = sparkWoker.actorOf(ClusterClient.props(ClusterClientSettings(sparkWoker).withInitialContacts(initialContacts)),"sparkWoker")

  def getActorRefAndSend(send:String,path:String)={
    actorRef ! Send(send,ClusterMessage(path),true)
  }

}
